//
//  SharePopView.m
//  Douyin
//
//  Created by Qiao Shi on 2018/7/30.
//  Copyright © 2018年 Qiao Shi. All rights reserved.
//

#import "SharePopView.h"

@implementation SharePopView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        
        self.frame = ScreenFrame;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
        _container = [[UIView alloc] initWithFrame:CGRectMake(0, KScreen_H, KScreen_W, 220 + KSafeAreaBottomHeight)];
        
        _container.backgroundColor = viewback_black;
        [self addSubview:_container];
        
        
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:_container.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        _container.layer.mask = shape;
        
        UIBlurEffect *blurEffect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
        visualEffectView.frame = self.bounds;
        visualEffectView.alpha = 1.0f;
        [_container addSubview:visualEffectView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreen_W, 35)];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.text = @"更多操作";
        label.textColor = [UIColor whiteColor];
        label.font = FontBold(16);
        [_container addSubview:label];
        
        
       
        
//        UIScrollView *topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, KScreen_W, 90)];
//        topScrollView.contentSize = CGSizeMake(itemWidth * topIconsName.count, 80);
//        topScrollView.showsHorizontalScrollIndicator = NO;
//        topScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 30);
//        [_container addSubview:topScrollView];
//        
//        for (NSInteger i = 0; i < topIconsName.count; i++) {
//            ShareItem *item = [[ShareItem alloc] initWithFrame:CGRectMake(20 + itemWidth*i, 0, 48, 90)];
//            item.icon.image = [UIImage imageNamed:topIconsName[i]];
//            item.label.text = topTexts[i];
//            item.tag = i;
//            [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onShareItemTap:)]];
//            [item startAnimation:i*0.03f];
//            [topScrollView addSubview:item];
//        }
//        
//        UIView *splitLine = [[UIView alloc] initWithFrame:CGRectMake(0, 130, KScreen_W, 0.5f)];
//        splitLine.backgroundColor = COLOR_ALPH(255.0, 255.0, 255.0, 0.1)
//;
//        [_container addSubview:splitLine];
        

        
        
        _cancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 170, KScreen_W, 50 + KSafeAreaBottomHeight)];
        [_cancel setTitleEdgeInsets:UIEdgeInsetsMake(-KSafeAreaBottomHeight, 0, 0, 0)];
        
        [_cancel setTitle:@"取消" forState:UIControlStateNormal];
        [_cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _cancel.titleLabel.font = Font(16);

        _cancel.backgroundColor = subviewback_black;
        [_container addSubview:_cancel];
        
        UIBezierPath* rounded2 = [UIBezierPath bezierPathWithRoundedRect:_cancel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape2 = [[CAShapeLayer alloc] init];
        [shape2 setPath:rounded2.CGPath];
        _cancel.layer.mask = shape2;
        [_cancel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
    }
    return self;
}
- (void)setBottomTexts:(NSArray *)bottomTexts{
    _bottomTexts = bottomTexts;
//    NSArray *bottomIconsName = @[
//                                @"football",
//                                @"football",
//                                @"football",
//                                @"football"
//                                ];
//    NSArray *bottomTexts = @[
//                            @"举报",
//                            @"保存至相册",
//                            @"复制链接",
//                            @"不感兴趣"
//                            ];
    CGFloat itemWidth = 68;
    UIScrollView *bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, KScreen_W, 90)];
    bottomScrollView.contentSize = CGSizeMake(itemWidth * _bottomIconsName.count, 80);
    bottomScrollView.showsHorizontalScrollIndicator = NO;
    bottomScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 30);
    [_container addSubview:bottomScrollView];
    
    for (NSInteger i = 0; i < _bottomIconsName.count; i++) {
        ShareItem *item = [[ShareItem alloc] initWithFrame:CGRectMake(20 + itemWidth*i, 0, 48, 90)];
        item.icon.image = [UIImage imageNamed:_bottomIconsName[i]];
        item.label.frame = CGRectMake(0, 64, 48, 25);
        item.icon.frame = CGRectMake(8, 16, 32, 32);
//        if([_bottomIconsName[i] isEqualToString:@"ico_mine_more_share"]){
//            item.icon.frame = CGRectMake(9, 17, 30, 30);
//        }else if([_bottomIconsName[i] isEqualToString:@"ico_mine_more_jubao"]){
//            item.icon.frame = CGRectMake(9, 17, 30, 30);
//        }else if([_bottomIconsName[i] isEqualToString:@"ico_mine_more_notlike"]){
//            item.icon.frame = CGRectMake(9, 17, 30, 30);
//        }else if([_bottomIconsName[i] isEqualToString:@"ico_mine_more_download"]){
//            item.icon.frame = CGRectMake(9, 17, 30, 30);
//        }else if([_bottomIconsName[i] isEqualToString:@"ico_mine_more_delete"]){
//            item.icon.frame = CGRectMake(9, 17, 30, 30);
//        }else if([_bottomIconsName[i] isEqualToString:@"ico_mine_more_back"]){
//            item.icon.frame = CGRectMake(9, 17, 30, 30);
//        }
//        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.height.mas_equalTo(48);
//            make.centerX.equalTo(self);
//            make.top.equalTo(self).offset(10);
//        }];
        item.label.text = bottomTexts[i];
        item.tag = i;
        [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onActionItemTap:)]];
        [item startAnimation:i*0.03f];
        [bottomScrollView addSubview:item];
    }
}
- (void)onShareItemTap:(UITapGestureRecognizer *)sender {
   
    switch (sender.view.tag) {
        case 0:
            break;
        default:
            break;
    }
    
    [self dismiss];
}
- (void)onActionItemTap:(UITapGestureRecognizer *)sender {
    
    [self.delegate actionClickedAtIndex:sender.view.tag type:_bottomTexts[sender.view.tag]];
    
    [self dismiss];
}

- (void)handleGuesture:(UITapGestureRecognizer *)sender {
    CGPoint point = [sender locationInView:_container];
    if(![_container.layer containsPoint:point]) {
        [self dismiss];
        return;
    }
    point = [sender locationInView:_cancel];
    if([_cancel.layer containsPoint:point]) {
        [self dismiss];
    }
}

- (void)show {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self];
    [UIView animateWithDuration:0.15f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGRect frame = self.container.frame;
                         frame.origin.y = frame.origin.y - frame.size.height;
                         self.container.frame = frame;
                     }
                     completion:^(BOOL finished) {
                     }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.15f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         CGRect frame = self.container.frame;
                         frame.origin.y = frame.origin.y + frame.size.height;
                         self.container.frame = frame;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}

@end



#pragma Item view

@implementation ShareItem
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _icon = [[UIImageView alloc] init];
        _icon.image = [UIImage imageNamed:@"football"];
        _icon.contentMode = UIViewContentModeScaleToFill;
        _icon.userInteractionEnabled = YES;
        [self addSubview:_icon];
        
        _label = [[UILabel alloc] init];
        _label.text = @"TEXT";
        _label.textColor = COLOR_ALPH(255.0, 255.0, 255.0, 1);
        _label.font = FontMedium(15);
        _label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_label];
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
    }
    return self;
}
-(void)startAnimation:(NSTimeInterval)delayTime {
    CGRect originalFrame = self.frame;
    self.frame = CGRectMake(CGRectGetMinX(originalFrame), 35, originalFrame.size.width, originalFrame.size.height);
    [UIView animateWithDuration:0.9f
                          delay:delayTime
         usingSpringWithDamping:0.5f
          initialSpringVelocity:0.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.frame = originalFrame;
                     }
                     completion:^(BOOL finished) {
                     }];
}

-(void)layoutSubviews {
    [super layoutSubviews];
//    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(48);
//        make.centerX.equalTo(self);
//        make.top.equalTo(self).offset(10);
//    }];
//    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self);
//        make.top.equalTo(self.icon.mas_bottom).offset(10);
//    }];
}

@end

